package com.agregatesdk.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class HttpUtil {
    public static boolean addCredit4User(String packagename, Context context, float credit, int offerType) throws Throwable {

        UserAccount.getInstance().init(context);

        boolean flg = false;
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(Constant.DEVICE_ID, UserAccount.getInstance().getDeviceid()));
        params.add(new BasicNameValuePair(Constant.IP, UserAccount.getInstance().getIp()));
        params.add(new BasicNameValuePair(Constant.UID, UserAccount.getInstance().getUid()));
        params.add(new BasicNameValuePair(Constant.APP_PACKAGENAME, packagename));

        long serverTime = System.currentTimeMillis();
        params.add(new BasicNameValuePair(Constant.SERVER_TIME, String.valueOf(serverTime)));
        params.add(new BasicNameValuePair(Constant.TOKEN, Utils.generateToken(String.valueOf(serverTime))));
        params.add(new BasicNameValuePair(Constant.VERSIONCODE, String.valueOf(UserAccount.getInstance().getVersionCode())));
        params.add(new BasicNameValuePair(Constant.CREDIT, Constant.moneysizeFormat1.format(((((double)credit)/100)))));
        params.add(new BasicNameValuePair(Constant.OFFER_TYPE, String.valueOf(offerType)));

        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        final HttpPost post = new HttpPost(Constant.URL_ADD_CREDIT);
        post.setEntity(entity);

        HttpResponse httpResponse = getHttpClient().execute(post);

        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            entity = httpResponse.getEntity();
            String response = null;

            response = EntityUtils.toString(entity, "UTF-8");

            Log.d("==============================", response);
            JSONObject responsO = new JSONObject(response);
            if (responsO.getString("status").equals("200")) {
                flg = true;
                UserAccount.getInstance().setAmount(responsO.getString("amount") == null ? "0" : responsO.getString("amount"));
                UserAccount.getInstance().setRest(responsO.getString("rest") == null ? "0" : responsO.getString("rest"));
            }
        }
        return flg;
    }

    public static HttpClient getHttpClient() {
        HttpClient httpClient = new DefaultHttpClient();

        final HttpParams params = httpClient.getParams();
        // 设置网络与服务器建立连接的超时时间,即socket连接的超时时间(3sec)
        HttpConnectionParams.setConnectionTimeout(params, Constant.HTTP_REQUEST_TIMEOUT_MS);
        // Socket读数据的超时时间，即从服务器获取响应数据需要等待的时间
        HttpConnectionParams.setSoTimeout(params, Constant.HTTP_REQUEST_TIMEOUT_MS);
        // // ConnectionManager管理的连接池中取出连接的超时时间
        // ConnManagerParams.setTimeout(params, HTTP_REQUEST_TIMEOUT_MS);

        return httpClient;
    }
}
